package com.wanmait.movies.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.movies.dao.CollectDao;
import com.wanmait.movies.dao.ConcernedDao;
import com.wanmait.movies.dao.DiscussDao;
import com.wanmait.movies.dao.MovieDao;
import com.wanmait.movies.dao.UserDao;
import com.wanmait.movies.vo.Collect;
import com.wanmait.movies.vo.Concerned;
import com.wanmait.movies.vo.Discuss;
import com.wanmait.movies.vo.Movie;
import com.wanmait.movies.vo.Pager;
import com.wanmait.movies.vo.User;

/**
 * Servlet implementation class TaRen
 */
@WebServlet("/TaRenServlet")
public class TaRenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TaRenServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		if(("userPage").equals(action)) {
			this.userPage(request, response);
		}
		if ("userCollectAjax".equals(request.getParameter("action"))) {
			userCollectAjax(request, response);
		}
		if ("userConcernAjax".equals(request.getParameter("action"))) {
			userConcernAjax(request, response);
		}
		if("concernedUser".equals(request.getParameter("action"))) {
			concernedUser(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	//关注和取消关注
	protected void concernedUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter pw =response.getWriter();
		
		String html =request.getParameter("html");
		String uid =request.getParameter("uid");
		HttpSession session = request.getSession();
		User user =(User)session.getAttribute("ui");
		
		if(html.equals("关注")) {
			
		ConcernedDao.getConcerned().insertConcerned(user.getId(), Integer.parseInt(uid));	
		pw.print("0");
		}
        if(html.equals("取消关注")) {
		ConcernedDao.getConcerned().ConcernedDelete(user.getId(), Integer.parseInt(uid));	
		pw.print("1");	
		}
	}
	
	
	protected void userConcernAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager = new Pager();
		String userId = request.getParameter("userId");
		pager.setCurrentPager(Integer.parseInt(request.getParameter("currentPager")));
		pager.setPagerNum(ConcernedDao.getConcerned().getPageMax(Integer.parseInt(userId)));
		pager.setCountinPager(pager.getPagerNum()/5+(pager.getPagerNum()%5==0?0:1));
		ArrayList<Concerned> concerneds = ConcernedDao.getConcerned().getMyConcernsed(Integer.parseInt(userId), pager.getCurrentPager());
		request.setAttribute("userconcerns", concerneds);
		request.setAttribute("pagerofconcern", pager); 
		request.getRequestDispatcher("/list.jsp").forward(request, response);
	}
	
	
	protected void userCollectAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager = new Pager();
		String userId = request.getParameter("userId");
		pager.setCurrentPager(Integer.parseInt(request.getParameter("currentPager")));
		pager.setPagerNum(CollectDao.getObj().getPageMax(Integer.parseInt(userId)));
		pager.setCountinPager(pager.getPagerNum()/5+(pager.getPagerNum()%5==0?0:1));
		ArrayList<Collect> allcollects = CollectDao.getObj().getcollectdao(pager, Integer.parseInt(userId));
		request.setAttribute("usercollects", allcollects);
		request.setAttribute("pagerofcollect", pager); 
		request.getRequestDispatcher("/list.jsp").forward(request, response);
	}
	protected void userPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		String uz = request.getParameter("uzi");
		User uzi = UserDao.getObj().getUser(Integer.parseInt(uz));
		HttpSession session = request.getSession();
	    User user=	(User)session.getAttribute("ui");
		//获得是否关注
		Concerned concerned = ConcernedDao.getConcerned().getIsConcerned(user.getId(), Integer.parseInt(uz));
		request.setAttribute("concerned", concerned); 
		
		
		Pager pagerofcollect = new Pager();
		pagerofcollect.setCurrentPager(0);
		pagerofcollect.setPagerNum(CollectDao.getObj().getPageMax(uzi.getId()));//
		pagerofcollect.setCountinPager(pagerofcollect.getPagerNum()/5+(pagerofcollect.getPagerNum()%5==0?0:1));
		ArrayList<Collect> collects = null;
		if (pagerofcollect.getPagerNum()!=0) {
			 collects = MovieDao.getMovieDao().getUserCollects(uzi.getId());
		}
		
		request.setAttribute("pagerofcollect", pagerofcollect); 
		request.setAttribute("usercollects", collects);
		
		Pager pagerofconcern = new Pager();
		pagerofconcern.setCurrentPager(1);
		pagerofconcern.setPagerNum(ConcernedDao.getConcerned().getPageMax(uzi.getId()));
		pagerofconcern.setCountinPager(pagerofconcern.getPagerNum()/5+(pagerofconcern.getPagerNum()%5==0?0:1));
		ArrayList<Concerned> concerneds = null;
		if (pagerofconcern.getPagerNum() != 0) {
			concerneds = ConcernedDao.getConcerned().getMyConcernsed(uzi.getId(), pagerofconcern.getCurrentPager());
		}
		request.setAttribute("pagerofconcern", pagerofconcern);
		request.setAttribute("userconcerns", concerneds);
		
		Pager pageofcomment = new Pager();
		pageofcomment.setCurrentPager(1);
		pageofcomment.setPagerNum(DiscussDao.getObj().getpagemax(uzi.getId()));
		pageofcomment.setCountinPager(pageofcomment.getPagerNum()/5+(pageofcomment.getPagerNum()%5==0?0:1));
		ArrayList<Discuss> discusses = null;
		if (pageofcomment.getPagerNum() != 0 ) {
			discusses = DiscussDao.getObj().alldiscuss(uzi.getId(), pageofcomment.getCurrentPager());
		}
		request.setAttribute("pagerofcomment", pageofcomment);
		request.setAttribute("usercomments", discusses);
		
		Pager pagerofrecomment = new Pager();
		pagerofrecomment.setCurrentPager(1);
		pagerofrecomment.setPagerNum(DiscussDao.getObj().repagemax(uzi.getId()));
		pagerofrecomment.setCountinPager(pagerofrecomment.getPagerNum()/5+(pagerofrecomment.getPagerNum()%5==0?0:1));
		ArrayList<Discuss> discusses2 = null;
		if (pagerofrecomment.getPagerNum() != 0) {
			discusses2 = DiscussDao.getObj().rediscuss(uzi.getId(), pagerofrecomment.getCurrentPager());
		}
		request.setAttribute("pagerofrecomment", pagerofrecomment);
		request.setAttribute("userrecomments", discusses2);
		request.setAttribute("uzi", uzi);
		request.getRequestDispatcher("/taren.jsp").forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
